package com.payment.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.payment.common.base.AjaxResult;
import com.payment.common.support.Convert;
import com.payment.common.type.MerchantImgStatus;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.MerchantImage;
import com.payment.system.mapper.MerchantImageMapper;
import com.payment.system.service.IMerchantImageService;


/**
 * 商户资质 服务层实现
 * 
 * @author payment
 * @date 2019-01-11
 */
@Service
public class MerchantImageServiceImpl implements IMerchantImageService 
{
	@Autowired
	private MerchantImageMapper merchantImageMapper;

	/**
     * 查询商户资质信息
     * 
     * @param type 商户资质ID
     * @return 商户资质信息
     */
    @Override
	public MerchantImage selectMerchantImageById(String type)
	{
	    return merchantImageMapper.selectMerchantImageById(type);
	}
	
	/**
     * 查询商户资质列表
     * 
     * @param merchantImage 商户资质信息
     * @return 商户资质集合
     */
	@Override
	public List<MerchantImage> selectMerchantImageList(MerchantImage merchantImage)
	{
	    return merchantImageMapper.selectMerchantImageList(merchantImage);
	}
	
    /**
     * 新增商户资质
     * 
     * @param merchantImage 商户资质信息
     * @return 结果
     */
	@Override
	public int insertMerchantImage(MerchantImage merchantImage)
	{
	    return merchantImageMapper.insertMerchantImage(merchantImage);
	}
	
	/**
     * 修改商户资质
     * 
     * @param merchantImage 商户资质信息
     * @return 结果
     */
	@Override
	public int updateMerchantImage(MerchantImage merchantImage)
	{
	    return merchantImageMapper.updateMerchantImage(merchantImage);
	}

	/**
     * 删除商户资质对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteMerchantImageByIds(String ids)
	{
		return merchantImageMapper.deleteMerchantImageByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<MerchantImage> selectMerchantImageListByMerchantId(String merchantId) {
		return merchantImageMapper.selectMerchantImageListByMerchantId(merchantId);
	}

	@Override
	public int  insertMerchantImageByMerchantId(String fileUrl, String merchantId,String imgType) {
		MerchantImage merchantImage=new MerchantImage();
		merchantImage.setCreateUserId(merchantId);
		merchantImage.setId(StringUtils.randomUUID());
		merchantImage.setType(imgType);
		merchantImage.setImageUrl(fileUrl);
		merchantImage.setMerchantId(merchantId);
		return merchantImageMapper.insertMerchantImage(merchantImage);
	}

	//  //app 修改用户头像
	@Override
	public AjaxResult updateHeadPortrait(String merchantId, String fileUrl) {
		if(StringUtils.isEmpty(merchantId)||StringUtils.isEmpty(fileUrl)){
			return  AjaxResult.error(1,"缺失参数");
		}
		List<MerchantImage> list=merchantImageMapper.selectMerchantImageListByMerchantIdAndType(merchantId, MerchantImgStatus.HI.getCode());
		if(list!=null&&list.size()>0){
			merchantImageMapper.deleteMerchantImageByMerchantIdAndType(merchantId,MerchantImgStatus.HI.getCode());
		}
		this.insertMerchantImageByMerchantId(fileUrl,merchantId, MerchantImgStatus.HI.getCode());
		List<MerchantImage> listImg=merchantImageMapper.selectMerchantImageListByMerchantIdAndType(merchantId,MerchantImgStatus.HI.getCode());


		////修改图片访问路径
		if(listImg!=null&&listImg.size()>0){
			for(MerchantImage image:listImg){
				image.setImageUrl(image.getImageUrl());
			}
		}

		return AjaxResult.successAndData("修改头像成功",listImg);

	}

	@Override
	public List<MerchantImage> selectMerchantImageListByMerchantIdAndType(String merchantId, String type) {
		return merchantImageMapper.selectMerchantImageListByMerchantIdAndType(merchantId,type);
	}

	@Override
	public List<MerchantImage> selectMerchantImageListByMerchantIdAndTypeTWO(String merchantId, String type) {
		return merchantImageMapper.selectMerchantImageListByMerchantIdAndTypeTWO(merchantId,type);
	}


}
